package com.example.luyao.faceservertest;

import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.Retrofit;

public class Utils {
    public static final SimpleHttpClient.ServerAPI getHttpClient(int timeout) {
        final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(timeout, TimeUnit.SECONDS)
                .writeTimeout(timeout, TimeUnit.SECONDS)
                .readTimeout(timeout, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).baseUrl(SimpleHttpClient.BASE_URL).build();
        SimpleHttpClient.ServerAPI service = retrofit.create(SimpleHttpClient.ServerAPI.class);
        return service;
    }

    public static final JSONObject parseOkhttp3Response(okhttp3.Response response, String TAG) {
        int code = response.code();
        //Log.d(TAG, code + response.toString());
        JSONObject responseJson = new JSONObject();
        try {
            if (code == 200) {
                String response_str = response.body().string();
                responseJson = new JSONObject(response_str);
                //Log.d(TAG, responseJson.toString());

            } else {
                String error_str = response.message();
                //error_str = error_str.replaceAll("\"", "\\\\\"");
                responseJson = new JSONObject(error_str);
                //Log.e(TAG, responseJson.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return responseJson;
    }

    public static final JSONObject parseResponse(Response<ResponseBody> response, String TAG) {
        int code = response.code();
        //Log.d(TAG, code + response.toString());
        JSONObject responseJson = new JSONObject();
        try {
            if (code == 200) {
                String response_str = response.body().string();
                responseJson = new JSONObject(response_str);
                //Log.d(TAG, responseJson.toString());

            } else {
                String error_str = response.errorBody().string();
                //error_str = error_str.replaceAll("\"", "\\\\\"");
                responseJson = new JSONObject(error_str);
                //Log.e(TAG, responseJson.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return responseJson;
    }
}